//
//  PigRecordViewController.m
//  PigMarket
//
//  Created by mac on 2019/7/3.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PigRecordViewController.h"
#import "PigRecordCell.h"
#import "GrowRecordViewController.h"

@interface PigRecordViewController ()

@property (nonatomic , strong) NSMutableArray *dataArr;
@property (nonatomic , strong) UIImageView *bgImg;

@end

@implementation PigRecordViewController

#pragma mark - view life cycle

- (void)setupUI
{
    {
        self.baseNavView.navTitleLabel.text = @"档案";
        self.baseNavView.backgroundColor = UIColorFromRGB(0x53CD54);
        self.baseNavView.line.hidden = YES;
        [self.baseNavView.leftNavBtn setImage:[UIImage imageNamed:@"nav_white"] forState:UIControlStateNormal];
        
        [self.baseNavView.navTitleLabel setTextColor:KWhiteColor];
        
        [self.baseNavView.rightNavBtn setImage:[UIImage imageNamed:@"share_icon_white"] forState:UIControlStateNormal];
        self.baseNavView.rightNavBtn.hidden = NO;
        
        @weakify(self);
        self.baseNavView.rightBlock = ^{
            
        };
    }

    {
        [self.view addSubview:self.bgImg];
        [self.bgImg addSubview:self.tableView];
    }
}

- (void)setupLayout
{
    [self.bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(TJ_NavBarHeight, 0, 0, 0));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(30, 12, 20, 12));
    }];

}

- (void)initData {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] pigRecordWithPigId:self.pigId memberId:memberId] subscribeNext:^(PigRecord * _Nullable pigRecord) {
        @strongify(self);
        [self.dataArr addObject:pigRecord];
        [self.tableView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 9;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        return ScaleHeight(194);
    } else {
        return 46;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PigRecordCell *cell = [PigRecordCell cellWithTableView:tableView];
    
    if (self.dataArr.count > 0) {
        [cell configCellWithModel:self.dataArr[0] index:indexPath];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        GrowRecordViewController *vc = [[GrowRecordViewController alloc] init];
        vc.pigId = self.pigId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark - getter and setter

- (void)registerTableViewCell{
    
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_header.hidden = YES;
    [self addShadowAndCircleCorner:self.tableView.layer corner:5];
    
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (UIImageView *)bgImg {
    if (!_bgImg) {
        _bgImg = [[UIImageView alloc] init];
        UIImage *img = [UIImage imageNamed:@"adopted_record_bg"];
        _bgImg.userInteractionEnabled = YES;
        //设置不被拉伸区域
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(100, 50, 50, 50) resizingMode:UIImageResizingModeStretch];
        _bgImg.image = img;
    }
    return _bgImg;
}


@end


